﻿///Author:Jancion;Time:070615

importClass('rapi.rbase.RDropList');
importClass('rapi.rbase.RDiv');
importClass('rapi.rio.RSearchEngine');

function RCategoryPicker(rootCategoryId,maxLevel){
	this.toString=function(){
		return 'RCategoryPicker';
	}
	
	this.rootCategoryId=rootCategoryId;
	this.maxLevel=maxLevel;
	this.engine=new RSearchEngine(JS_SERVER+'GetXml.aspx');
	
	this.droplists=new Array();
	this.categories=new Array();
	
	this.div=new RDiv(0,0,null,null).get();
	this.style=this.div.style;
	    
	this.categoryId=new Array();
	this.level=0;
	
	this.pickerWidth  = null;
	
	this.setPickerWidth = function ( nWidth ){
	    if( nWidth > 50 ){
	        this.pickerWidth = nWidth;
	    }
	}
	
	this.initPicker=function(rootId){
	    if(rootId!=-2){
	        if(this.level<this.maxLevel){
			    this.loading=true;
			    this.level++;
			    this.engine.getSubCategories(rootId,this,this.categoryCallBack);
		    }else{
			    this.loading=false;
		    }
	    }		
	}
	
	this.categoryCallBack=function(){
		var xmlHttp=this.engine.getXmlHttp();
		if(xmlHttp.readyState==4||xmlHttp.readyState=='complete'){
			if(xmlHttp.status==200){
				var xmldoc=xmlHttp.responseXML;
				var codenodes=xmldoc.getElementsByTagName("TYPEID");
				var pcodenodes=xmldoc.getElementsByTagName("PARENTID");
				var namenodes=xmldoc.getElementsByTagName("TYPE_NAME");		
				if(codenodes.length==0){
					while(this.droplists.length>this.level-1){
						var droplist=this.droplists.pop();
						droplist.clear();
						this.div.removeChild(droplist.get());
					}
					this.loading=false;
				}else{
					var droplist;
					if(!this.droplists[this.level-1]){
						droplist=new RDropList(0,0,this.pickerWidth,null);
						droplist.style.position='static';						
						this.droplists.push(droplist);
						droplist.level=this.droplists.length;
						this.div.appendChild(droplist.get());   
					}else{
						droplist=this.droplists[this.level-1];
						droplist.clear();
					}
					droplist.addListItem("全部类别",-1);
					for(var iCode=0;iCode<codenodes.length;iCode++){
						var code=codenodes[iCode].firstChild.nodeValue;
						var pcode=pcodenodes[iCode].firstChild.nodeValue;
						var name=namenodes[iCode].firstChild.nodeValue;						
						droplist.addListItem(name,code);
					}
					if(this.rootCategoryId==-2){
					    this.droplists[this.level-1].select.value=this.categoryId[this.level].toString(10);
					    this.droplists[this.level-1].selectedValue=this.categoryId[this.level].toString(10);
					}
					var listlistener=new Object();
					listlistener.root=this;
					listlistener.target=droplist;
					listlistener.onselectchanged=function(){
						this.root.level=this.target.level;						
						this.root.initPicker(this.target.selectedValue);
						setRTimeout(this.root,this.root.notifychanged,100);
					}
					droplist.addListener(listlistener,'onselectchanged');					
					
					if(this.rootCategoryId==-2){
					    this.initDefaultDistrictById();
					}else{
					    this.initPicker(droplist.selectedValue);
					}
				}
			}
		}
	}
	
	this.initPicker(this.rootCategoryId);
	
	this.initDefaultDistrictById=function(){
	    if(this.level<this.categoryId.length-1){
		    this.loading=true;
		    this.level++;
		    this.engine.getSubCategories(this.categoryId[this.level-1],this,this.categoryCallBack);
	    }else{
		    this.loading=false;
		    this.categoryId=new Array();
		    this.rootCategoryId=0;		    
	    }
	}
	
	this.setDefaultDistrictById=function(categoryId){		
		this.categoryId=categoryId;
		this.initDefaultDistrictById();
	}
	
	this.setDefaultCategoryByName=function(CategoryName){
	}
	
	this.getCurrentCategoryName=function(){
		if(this.droplists[this.droplists.length-1].selectedText=="全部类别"&&this.droplists.length!=1){
			return this.droplists[this.droplists.length-2].selectedText;
		}
		return this.droplists[this.droplists.length-1].selectedText;
	}
	this.getCurrentCategoryFullName=function(){
		var fullname='';
		for(var iList=0;iList<this.droplists.length;iList++){
		    if(this.droplists[iList].selectedText!="全部类别"){
		        fullname+=this.droplists[iList].selectedText;
		    }
		}
		return fullname;
	}
	this.getCurrentCategoryId=function(){
       if( this.droplists[this.droplists.length-1] != null){
            if(this.droplists[this.droplists.length-1].selectedValue!=-1){
                return this.droplists[this.droplists.length-1].selectedValue;
            }
            else{
                if(this.droplists.length<2){
                    if(this.droplists[this.droplists.length-1].selectedValue==-1){
                        return 0;
                    }
                    else{
                        return this.droplists[this.droplists.length-1].selectedValue;
                    }
                }
                else{
                    return this.droplists[this.droplists.length-2].selectedValue;
                }
            }
        }else{
            return 0;
        }
	}
	this.getCategoryById=function(id){
		for(var iCategory=0;iCategory<this.categories.length;iCategory++){
			if(this.categories[iCategory]==id){
				return this.categories[iCategory];
			}
		}
		return null;
	}
	this.get=function(){
		return this.div;
	}
	
	this.onCategoryChanged=new Array();
	this.addListener=function(listener, estring){
		switch(estring){
			case 'onCategoryChanged':
				if(listener.onCategoryChanged){
					this.onCategoryChanged[this.onCategoryChanged.length]=listener;
				}else{				    
					window.status=this.toString()+":不合法的"+estring+"事件对象";
				}
			break;
			default:
				window.status=this.toString()+"类不存在"+estring+"事件";
			break;
		}
	}
	this.notify=function(estring){
		switch(estring){
			case 'onCategoryChanged':
				for(var i=0;i<this.onCategoryChanged.length;i++){
					if(this.onCategoryChanged[i].onCategoryChanged){
						this.onCategoryChanged[i].onCategoryChanged();
					}else{
						window.status=this.toString()+'类的某'+estring+'事件监听对象损坏';
					}
				}
			break;
			default:
				window.status=this.toString()+'类不存在'+estring+'事件';
			break;
		}
	}
	this.notifychanged=function(){
		if(this.loading){
			setRTimeout(this,this.notifychanged,100);
		}else{
			this.notify('onCategoryChanged');
		}
	}
}